<?php
/**
 * User: Allen
 * Date: 15-07-11
 * Time: 14:21
 * 项目
 */

class Project extends PC_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->model('project_model');
		$this->load->model('submenu_model');
	}
	
	//项目列表
	public function project_list(){
		//获取总数
		$this->data['count'] = $this->project_model->getCount();
		if($this->data['count']) {
			$this->data['data'] = $this->project_model->getProjectlist();
			foreach($this->data['data'] as $key => $value) {
				if($value['img']) {
					$this->data['data'][$key]['img'] = '<img src="'.$value['img'].'" style="width:80px;height:80px;">';
				}else {
					$this->data['data'][$key]['img'] = '<img src="/public_source/www/assets/images/default.jpg" style="width:80px;height:80px;">';
				}
				if($value['show_flag'] == 1) {
					$this->data['data'][$key]['show_flag'] = '展示';
				}else {
					$this->data['data'][$key]['show_flag'] = '不展示';
				}
			}
		}else {
			$this->data['data'] = array();
		}
		$this->load->view('/www/project_list.php',$this->data);
	}
	
	//读取信息进入编辑页面
	public function edit_page() {
		$id = $this->uri->segment(4);
		$where['id'] = $id;
		$project_info = $this->project_model->checkProject($where);
		$this->data['html'] = $project_info;
		$this->load->view('/www/project_edit',$this->data);
	}
	
	//删除操作
	public function del() {
		$id = $this->uri->segment(4);
		$where['id'] = $id;	
		$del_result = $this->project_model->deleteProject($where);
		$submenu_where['project_id'] = $id;
		$del_submenu = $this->submenu_model->deleteSubmenu($submenu_where);
		if($del_result && $del_submenu) {
			echo "<script>alert('删除成功');window.location.replace('/www/project/project_list');</script>";
		}else {
			echo "<script>alert('删除失败');window.location.replace('/www/project/project_list');</script>";
		}
	}
	
	//编辑操作
	public function edit() {		
		$data = $_POST;
		$error = 0;
		if(is_uploaded_file($_FILES['jietu']['tmp_name'])){ 
			$upfile=$_FILES["jietu"];
			//获取数组里面的值 
			$name=$upfile["name"];//上传文件的文件名 
			$type=$upfile["type"];//上传文件的类型 
			$size=$upfile["size"];//上传文件的大小 
			$tmp_name=$upfile["tmp_name"];//上传文件的临时存放路径 		  
			//取扩展名 
			$file_ext=explode(".",$name); 
			$file_ext=$file_ext[count($file_ext)-1]; 
			$file_ext=strtolower($file_ext);
			$newname=time().".".$file_ext; 
			$error=$upfile["error"];
			move_uploaded_file($tmp_name,'public_source/upload/'.$newname); 
			$destination = '/public_source/upload/'.$newname;
		}else {
			$destination = $data['img'];
		}
		if($error == 0){ 
			$data['img'] = $destination;
			$data['update_time'] = time();
			$where['id'] = $data['id'];
			$edit_result = $this->project_model->editProject($where,$data);	
			if($edit_result){
				echo "<script>alert('提交成功');window.location.replace('/www/project/project_list');</script>";
			}
		}else{ 
			echo "<script>alert('上传失败');window.location.replace('/www/project/project_list');</script>";
		}
	}
	
	//进入添加页面
	public function add_page() {
		$this->load->view('/www/project_add',$this->data);
	}

	
	//添加操作
	public function add() {
		$data = $_POST;
		$error = 0;
		$data['img'] = '';
		if(is_uploaded_file($_FILES['jietu']['tmp_name'])){
			$upfile=$_FILES["jietu"];
			//获取数组里面的值
			$name=$upfile["name"];//上传文件的文件名
			$type=$upfile["type"];//上传文件的类型
			$size=$upfile["size"];//上传文件的大小
			$tmp_name=$upfile["tmp_name"];//上传文件的临时存放路径
			//取扩展名
			$file_ext=explode(".",$name);
			$file_ext=$file_ext[count($file_ext)-1];
			$file_ext=strtolower($file_ext);
			$newname=time().".".$file_ext;
			$error=$upfile["error"];
			move_uploaded_file($tmp_name,'public_source/upload/'.$newname);
			$destination = '/public_source/upload/'.$newname;
		}else {
			$destination = $data['img'];
		}
		if($error == 0){
			$data['img'] = $destination;
			$data['create_time'] = $data['update_time'] = time();
			$add_result = $this->project_model->addProject($data);
			if($add_result){
				echo "<script>alert('添加成功');window.location.replace('/www/project/project_list');</script>";
			}
		}else{
			echo "<script>alert('上传失败');window.location.replace('/www/project/project_list');</script>";
		}
	}
	
	//项目列表
	function pettags() {
		$typelist = $this->project_model->getProjectlist();
		$datainfo = '';
		foreach($typelist as $key => $value){
			$datainfo .= '<a href="javascript:;" class="pet_tag">'.$value['name'].'</a>';
		}
		echo json_encode($datainfo);
	}
}